package com.itheima.service;

import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.MenuService;
import com.itheima.dao.MenuDao;
import com.itheima.dao.UserDao;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = MenuService.class)
@Transactional
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuDao menuDao;
    @Autowired
    UserDao userDao;

    /*返回树形列表数据，返回的是一个菜单List,每个菜单中又可能有子级菜单*/
    @Override
    public List<Menu> getAll() {
        /*查询零级菜单*/
        List<Menu> menus = menuDao.queryZeroMenus();

        if (CollectionUtils.isNotEmpty(menus)) {
            for (Menu menu : menus) {
                queryAll(menu);
            }
        }
//        //查询二级菜单
//        List<Menu> menusTwo = menuDao.queryTwoMenus();
        return menus;
    }

    /*递归*/
    private List<Menu> queryAll(Menu menu) {
        //查询一级菜单
        List<Menu> menusOne = menuDao.queryOneMenus(menu.getId());
        if (CollectionUtils.isNotEmpty(menusOne)) {
            menu.setChildren(menusOne);
            for (Menu menu1 : menusOne) {
                queryAll(menu1);
            }
        }
        return menusOne;
    }

    /*添加目录*/
    @Override
    public void add(HashMap map, String username) {
        User user = userDao.findByUserName(username);
        Integer userId = user.getId();
        menuDao.add(map);
        String name = (String) map.get("name");
        Integer parentMenuId = (Integer) map.get("parentMenuId");
        Menu menu = menuDao.findMenuByName(name);
        menuDao.addMenuAndRole(menu.getId(), userId);
    }

    @Override
    public Result delete(Integer id) {
        myDelete(id);
        return Result.success("删除成功");
    }
    public void myDelete(Integer id) {
        List<Integer> ids = menuDao.findChildrenIds(id);
        //    ids.add(id);
        if (ids != null && ids.size() > 0) {
            for (Integer menuId : ids) {
                myDelete(menuId);
            }
            Integer count = menuDao.findMenuAndRole(id);
            if (count > 0) {
                // return Result.error("当前菜单已被引用不能删除");
                menuDao.deleteRoleAndMenu(id);
            }
            menuDao.delete(id);
        } else {
            Integer count = menuDao.findMenuAndRole(id);
            if (count > 0) {
                // return Result.error("当前菜单已被引用不能删除");
                menuDao.deleteRoleAndMenu(id);
            }
            //删除当前菜单
            menuDao.delete(id);
        }
       /* //删除当前菜单的所有子菜单
        menuDao.deleteChildren(id);*/
    }

    @Override
    public Menu findMenu(Integer id) {
        Menu menu = menuDao.findMenu(id);
        List<Menu> Childrens = menuDao.findMenus();
        List<Integer> menuIds = menuDao.findMenuIds(id);
        menu.setMenuIds(menuIds);
        menu.setChildren(Childrens);
        return menu;
    }

    @Override
    public void edit(Menu menu) {
        Integer id = menu.getId();
        List<Integer> menuIds = menu.getMenuIds();
        for (Integer menuId : menuIds) {
            menuDao.addChildren(menuId, id);
        }
        menuDao.edit(menu);
    }

    @Override
    public List<Menu> findUserMenu(String username) {
        List<Menu> ParentMenus = menuDao.findParentMenu(username);
        for (Menu parentMenu : ParentMenus) {
            List<Menu> ChildrenMenus = menuDao.findChildrenMenu(parentMenu.getId());
            parentMenu.setChildren(ChildrenMenus);
        }
        return ParentMenus;
    }
}
